package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    @Override
    public void saveSetmeal(SetmealDTO dto) {

        //1.添加setmeal表，需要返回主键值，以便给setmeal_dish表添加外键
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        setmealMapper.saveSetmeal(setmeal);
        //2.添加setmeal_dish表
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        //添加
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
        }
        setmealDishMapper.saveSetmealDishes(setmealDishes);
    }

    @Override
    public PageResult getSetmeals(SetmealPageQueryDTO dto) {

        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page<SetmealVO> page = setmealMapper.getSetmeals(dto);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public SetmealVO getSetmealById(Integer id) {

        SetmealVO vo = new SetmealVO();
        //查询setmeal表的内容，
        //TODO  Setmeal setmeal = setmealMapper.getSetmealById(id); ->改为MP
        Setmeal setmeal = setmealMapper.getSetmealById(id);
//        Setmeal setmeal = setmealMapper.selectById(id);
        BeanUtils.copyProperties(setmeal, vo);
        // 然后根据setmeal.id查询setmeal_dish表所有关联的内容最后合并在一起返回
        List<SetmealDish> setmealDishes = setmealDishMapper.getSetmealById(id);
        if (setmealDishes != null && setmealDishes.size() > 0) {
            vo.setSetmealDishes(setmealDishes);
        }
        return vo;
    }

    @Override
    @Transactional
    public void updateSetmeal(SetmealDTO dto) {
        //首先修改setmeal表中的内容
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        setmealMapper.updateSetmeal(setmeal);
        //然后删除setmeal_dish表中关联的所有内容
        setmealDishMapper.deleteSetmealDishById(dto);
        //最后重新添加setmeal_dish表中的内容
        if(dto.getSetmealDishes() != null && dto.getSetmealDishes().size() > 0) {
            //为setmeal_dish中每一个外键赋值
            for (SetmealDish smd : dto.getSetmealDishes()) {
                smd.setSetmealId(setmeal.getId());
            }
            setmealDishMapper.saveSetmealDishes(dto.getSetmealDishes());
        }
    }

    @Override
    public void deleteSetmeals(List<Long> ids) {
        //1.首先删除setmeal表中的内容
        setmealMapper.deleteSetmeals(ids);
        //2.然后根据ids删除setmeal_dish表中的内容
        setmealDishMapper.deleteSetmeals(ids);
    }

    @Override
    @Transactional
    public void updateSetmealStatus(Integer status, Long id) {
        //1.修改setmeal表的售货状态
        Setmeal setmeal = Setmeal.builder().id(id).status(status).build();
        setmealMapper.updateSetmeal(setmeal);
        //2.如果status=1，那么需要查询该套餐关联的所有食品，都需要是起售状态，如果有一个不是则抛出异常,setmeal_id查dish_id
        if(StatusConstant.ENABLE == status) {
            List<Long> dishIds = setmealDishMapper.getDishIdsBySetmealId(id);
            if(dishIds != null && dishIds.size() > 0) {
                //3.查找所有关联菜品
                List<Dish> dishs = dishMapper.getDishByIds(dishIds);
                for (Dish dish : dishs) {
                    if(StatusConstant.DISABLE == dish.getStatus()) {
                        throw new BaseException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                }
            }
        }

    }

    @Override
    public List<Setmeal> getSetmealByCategoryId(Integer categoryId) {
        List<Setmeal> setmeals = setmealMapper.getSetmealByCategoryId(categoryId);
        return setmeals;
    }

    @Override
    public List<DishItemVO> getDishBySetmealId(Integer id) {
        List<DishItemVO> dishItemVOS = setmealDishMapper.getDishBySetmealId(id);
        return dishItemVOS;
    }
}
